"""
@文件：6.进程池的实现方式.py
@作者：Miss丶念
@时间：2025/3/9：21:53
"""

import time
# from multiprocessing import Pool # 一般不用这种方式创建进程池，会与线程出现兼容问题
from concurrent.futures import ProcessPoolExecutor as Pool
from multiprocessing import Manager


def read(q):
    while True:
        message = q.get()
        if message is not None:
            print(f"read正在执行{message}")

        else:
            break
    print(f"read执行结束")


def write(q):
    for i in range(5):
        q.put(i)
        print(f"write正在执行{i}")
        time.sleep(0.5)
    q.put(None)
    print(f"write执行结束")


def main():
    print("主进程启动")
    with Manager() as manager:
        queue = manager.Queue()
        with Pool() as pool:
            pool.submit(read, queue)
            pool.submit(write, queue)



if __name__ == '__main__':
    main()


